#region Script Settings
#<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">
#  <ScriptPackager>
#    <process>powershell.exe</process>
#    <arguments />
#    <extractdir>%TEMP%</extractdir>
#    <files />
#    <usedefaulticon>true</usedefaulticon>
#    <showinsystray>false</showinsystray>
#    <altcreds>false</altcreds>
#    <efs>true</efs>
#    <ntfs>true</ntfs>
#    <local>false</local>
#    <abortonfail>true</abortonfail>
#    <product />
#    <version>1.0.0.1</version>
#    <versionstring />
#    <comments />
#    <company />
#    <includeinterpreter>false</includeinterpreter>
#    <forcecomregistration>false</forcecomregistration>
#    <consolemode>false</consolemode>
#    <EnableChangelog>false</EnableChangelog>
#    <AutoBackup>false</AutoBackup>
#    <snapinforce>false</snapinforce>
#    <snapinshowprogress>false</snapinshowprogress>
#    <snapinautoadd>2</snapinautoadd>
#    <snapinpermanentpath />
#    <cpumode>1</cpumode>
#    <hidepsconsole>false</hidepsconsole>
#  </ScriptPackager>
#</ScriptSettings>
#endregion

#
#
#
#	powershell -ExecutionPolicy Unrestricted .\Scripts\CreatePerformanceCounters.ps1
#

$categoryName = "RockBus"
 
$exists = [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)
if ($exists)
{
	[System.Diagnostics.PerformanceCounterCategory]::Delete($categoryName)
}


$OperationNames="ReceiveMessage","DeliverMessage","PersistMessage","PublishMessage","Routing.Queue.Processing","Routing.RetryQueue.Processing","Routing.ErrorQueue.Processing"

$counterData = New-Object System.Diagnostics.CounterCreationDataCollection

Write-Host "Creating performance counters:"

$OperationNames | ForEach-Object {
	$counter = New-Object System.Diagnostics.CounterCreationData
	$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::NumberOfItems32
	$counter.CounterName = [string]::Format("{0}.Executing", $_)
	$counter.CounterHelp = [string]::Format("Number of executing {0} operations", $_)
	[void]$counterData.Add($counter)
			 
	$counter = New-Object System.Diagnostics.CounterCreationData
	$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::NumberOfItems32
	$counter.CounterName = [string]::Format("{0}.Completed", $_)
	$counter.CounterHelp = [string]::Format("Number of completed {0} operations", $_)
	[void]$counterData.Add($counter)	
		 
	$counter = New-Object System.Diagnostics.CounterCreationData
	$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::NumberOfItems32
	$counter.CounterName = [string]::Format("{0}.Failed", $_)
	$counter.CounterHelp = [string]::Format("Number of failed {0} operations", $_)
	[void]$counterData.Add($counter)	
		 
	$counter = New-Object System.Diagnostics.CounterCreationData
	$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond32
	$counter.CounterName = [string]::Format("{0}.CompletionRate", $_)
	$counter.CounterHelp = [string]::Format("Number of completed {0} operations per second", $_)
	[void]$counterData.Add($counter)
		
	$counter = New-Object System.Diagnostics.CounterCreationData
	$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond32
	$counter.CounterName = [string]::Format("{0}.FailureRate", $_)
	$counter.CounterHelp = [string]::Format("Number of failed {0} operations per second", $_)
	[void]$counterData.Add($counter)
}

$counter = New-Object System.Diagnostics.CounterCreationData
$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::NumberOfItems64
$counter.CounterName = "UpTime"
$counter.CounterHelp = "Number of seconds this application has been running."
[void]$counterData.Add($counter)

[System.Diagnostics.PerformanceCounterCategory]::Create($categoryName, $categoryName, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counterData)

foreach ($counter in $counterData) { Write-Host("Performance counter: '{0}' was created" -f $counter.CounterName) }
